package com.base.data
{
	import com.base.data.account.AccountBean;
	import com.base.data.message.MessageBean;
	import com.base.data.sendrule.SendRuleBean;
	
	import flash.events.Event;
	
	import mx.collections.ArrayCollection;

	/**
	 * 数据中心
	 * @author Administrator
	 */	
	public class DataCenter
	{
		/**
		 *账号集合 
		 */
		[Bindable]
		public var accountList:ArrayCollection;
		
		/**
		 *帖子集合
		 */
		[Bindable]
		public var msgList:ArrayCollection;
		
		/**
		 *发送规则集合
		 */
		[Bindable]
		public var ruleList:ArrayCollection = new ArrayCollection();
	
		
		private static var _instance : DataCenter;
		
		public static function get instance() : DataCenter {
			if ( !_instance ) {
				_instance = new DataCenter();
			}
			return _instance;
		}
		
		public function DataCenter(){
			if( _instance ) throw new Error("请使用单例访问");
		}
		
		
		/**
		 * 初始化数据,请勿重复初始化
		 */		
		public function init( fun:Function = null ):void {
			
			accountList = File.read( File.File_Type_Account );
			msgList = File.read( File.File_Type_Message );
			ruleList = File.read( File.File_Type_Rule );
			if( fun != null ) fun();
		}
		
		/**
		 * 持久化数据
		 */		
		public function save( fun:Function = null ):void {
			File.write( File.File_Type_Account , accountList );
			File.write( File.File_Type_Message , msgList );
			File.write( File.File_Type_Rule , ruleList );
			if( fun != null ) fun();
		}
		
		/**
		 * 添加账号
		 * @param account
		 */
		public function addAccount( account:AccountBean ):void {
			accountList.addItem( account );
		}
		
		/**
		 * 修改账号
		 * @param account
		 */
		public function updateAccount( account:AccountBean ):void {
			delAccount( account.userName );
			addAccount( account );
		}
		
		/**
		 * 删除账号
		 * @param account
		 */		
		public function delAccount( userName:String ):void {
			
			for( var i:int = 0 ; i< accountList.length ; i++ ) {
				if( AccountBean(accountList[i]).userName == userName ) {
					accountList.removeItemAt( i ) ;
					break;
				}
			}
		}
		
		
		/**
		 * 添加帖子
		 * @param msg
		 */		
		public function addMessage( msg:MessageBean ):void {
			msgList.addItem( msg );
		}
		
		/**
		 * 修改帖子
		 * @param msg
		 */		
		public function updateMessage( msg:MessageBean ):void {
			delMessage( msg.msgNo );
			addMessage( msg );
		}
		
		/**
		 * 删除帖子 
		 * @param msgNo
		 */		
		public function delMessage( msgNo:String ):void {
			
			for( var i:int = 0 ; i< msgList.length ; i++ ) {
				if( MessageBean(msgList[i]).msgNo == msgNo ) {
					msgList.removeItemAt( i ) ;
					break;
				}
			}
			
		}
		
		/**
		 * 添加发送规则
		 * @param role
		 */		
		public function addSendRule( role:SendRuleBean ):void {
			ruleList.addItem( role );
		}
		
		/**
		 * 修改发送规则
		 * @param msg
		 */	
		public function updateSendRole( rule:SendRuleBean ):void {
			delSendRole( rule.ruleId );
			addSendRule( rule );
		}
		
		/**
		 * 删除发送规则
		 * @param msgNo
		 */		
		public function delSendRole( ruleId:String ):void {
			
			for( var i:int = 0 ; i < ruleList.length; i++ ) {
				if( SendRuleBean( ruleList[i] ).ruleId == ruleId ) {
					ruleList.removeItemAt( i );
					break;
				}
			}
		}
		
	}
}